﻿sampler2D _MainTex;

struct Input
{
    float2 uv_MainTex;
    float3 viewDir;
};

half _Glossiness;
half _Metallic;
fixed4 _Color;
half _FurLength; // 把FurLength统一，从外部通过Property传入，并通过FUR_LENGTH调整不同pass的毛发长度
half _CutOff;
half _CutOffEnd;
half _EdgeFade;
half3 _Gravity;
half _GravityStrength;

// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
// #pragma instancing_options assumeuniformscaling
UNITY_INSTANCING_BUFFER_START(Props)
    // put more per-instance properties here
UNITY_INSTANCING_BUFFER_END(Props)

void vert(inout appdata_full v)
{
    float3 dir = lerp(v.normal, _Gravity*_GravityStrength+v.normal*(1-_GravityStrength), FUR_LENGTH);
    v.vertex.xyz += dir * _FurLength * FUR_LENGTH;
}

void surf (Input IN, inout SurfaceOutputStandard o)
{
    // Albedo comes from a texture tinted by color
    fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    o.Albedo = c.rgb;
    // Metallic and smoothness come from slider variables
    o.Metallic = _Metallic;
    o.Smoothness = _Glossiness;

    //half4 lengthDetail = tex2D(_FurLengthDetail, IN.uv_MainTex);

    // 毛发遮罩，不同的pass，采用不同的FUR_LENGTH，因此，Cutoff也不同：
    o.Alpha = step(lerp(_CutOff,_CutOffEnd,FUR_LENGTH), c.a);

    // 与毛发长度相关
    half edgeFade = 1 - (FUR_LENGTH * FUR_LENGTH);

    // 与视角相关，物体边缘由于跟其他物体融合，效果较弱
    edgeFade += saturate(dot(IN.viewDir, o.Normal) - _EdgeFade); // [0,1]
    // 这里的代码可以整体注释掉，pow有开销的，各位同学自己再效果和性能上取得平衡，这里不赘述！
    edgeFade = saturate(pow(edgeFade,4)); // 浮点数精度会造成这里的异常，所以要加saturate
    //o.Albedo = edgeFade;

    o.Alpha *= edgeFade;
}